package day5.HomeWork;

import java.io.*;

/**
 * ClassName：Test2
 *
 * @Author：小鱼
 * @Create：2025-08-22 17:07
 */
public class Test2 {
    /*
    2.编写程序，按照GBK编码读取 D:\\test\\File_GBK.txt 文件内容，
    然后写入UTF-8编码文件 D:\\test\\File_UTF8.txt 。注意拷贝效率，注意新文件中不要出现多余的空行。
    */
    public static void main(String[] args) throws Exception{
        //按照GBK编码读取 D:\\test\\File_GBK.txt 文件内容，
        File file = new File("D:\\test\\File_GBK.txt");
        File file2 = new File("D:\\test\\File_UTF8.txt");
        FileInputStream iFile = new FileInputStream(file);
        FileOutputStream oFile = new FileOutputStream(file2);
        BufferedReader reader = new BufferedReader(new InputStreamReader(iFile, "GBK"));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(oFile,"UTF-8"));
        //逐行读取
        String line;
        while ((line = reader.readLine())!=null){
            //输出数据
            System.out.println("line: "+line);
            writer.write(line);
            if(reader.ready()){
                writer.newLine();//写入一个换行符
            }

        }
        //关闭资源
        writer.close();
        reader.close();
    }
}
